package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.domain.business.BusinessDo;
import com.qk.domain.business.BusinessFollwUp;
import com.qk.domain.business.BusinessPoolDto;
import com.qk.dto.clue.BusinessDto;
import com.qk.entity.Business;
import com.qk.management.anno.LogOperation;
import com.qk.management.constant.BusinessStatusConstants;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
@SuppressWarnings("all")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping
    @LogOperation
    public Result addBusinesses(@RequestBody BusinessDto bdo) {
        log.info("添加商机：{}", bdo);
        businessService.addBusiness(bdo);
        return Result.success();
    }

    @GetMapping
    public Result getBusinessesList(BusinessDo bd) {
        log.info("查询商机列表");
        PageResult<BusinessVo> list = businessService.getBusiness(bd);
        return Result.success(list);
    }

    @PutMapping("/assign/{businessId}/{userId}")
    @LogOperation
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机：{}", businessId, userId);
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable("id") Integer id) {
        log.info("查询商机：{}", id);
        BusinessVo businessVO = businessService.getBusinessById(id);
        return Result.success(businessVO);
    }

    @PutMapping
    @LogOperation
    public Result updateBusiness(@RequestBody BusinessFollwUp bfu) {
        log.info("更新商机：{}", bfu);
        businessService.updateById(bfu);
        return Result.success();
    }
    @PutMapping("/back/{id}")
    @LogOperation
    public Result backBusiness(@PathVariable("id") Integer id) {
        log.info("商机回退：{}", id);
        businessService.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.BACK)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    @LogOperation
    public Result toCustomer(@PathVariable("id") Integer id) {
        log.info("商机转客户：{}", id);
        businessService.toCustomerById(id);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result pool(BusinessPoolDto bpd) {
        log.info("获取线索池",bpd);
        PageResult<BusinessVo> pool = businessService.pool(bpd);
        return Result.success(pool);
    }
}
